package com.kim.authorization.server.config.extend;

import com.kim.oauth.common.model.UserAccount;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

/**
 * 集成认证-认证器抽象类
 */
@Slf4j
public abstract class AbstractPrepareIntegrationAuthenticator implements IntegrationAuthenticator {

    /**
     * 预处理打印认证方式
     * {@inheritDoc}
     */
    @Override
    public void prepare(IntegrationAuthenticationEntity entity) {
        log.info("IntegrationAuthenticator prepare this is authType -> {}", authTypeHandle(entity.getAuthType()));
    }

    /**
     * 认证结束后执行
     * {@inheritDoc}
     */
    @Override
    public void complete(IntegrationAuthenticationEntity entity) {
        log.info("IntegrationAuthenticator complete this is authType -> {}", authTypeHandle(entity.getAuthType()));
    }

    /**
     * 集成认证中除用户名密码不进行密码查询与校验
     *
     * @param userAccount 用户账户对象
     */
    void setEmptyPassword(UserAccount userAccount) {
        userAccount.setPassword("");
    }

    /**
     * 认证类型处理
     * <p>返回实际的认证类型</p>
     *
     * @param authType 认证类型
     */
    private String authTypeHandle(String authType) {
        return StringUtils.isEmpty(authType) ? "password" : authType;
    }
}
